/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Barrels;
import com.openbrew.openbrewrf.BrewNotes;
import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.Firkins;
import com.openbrew.openbrewrf.LossLog;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.BrewsTools;
import tools.InventoryModifiers;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.losslog")
public class LossLogFacadeREST extends AbstractFacade<LossLog> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public LossLogFacadeREST() {
        super(LossLog.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public LossLog create(@WebParam(name = "losslog") LossLog entity) {
        entity = super.create(entity);
        return entity;
    }
    
    @POST
    @Path("doFGILoss/{fgiID}/{fgiQty}")
    @Consumes({"application/xml", "application/json"})
    public LossLog createFGILoss(@PathParam("fgiID") int fgiID,
            @PathParam("fgiQty") int fgiQty, @WebParam(name = "losslog") LossLog entity) {
        final InventoryModifiers im = new InventoryModifiers();
        im.doFGIRemovalWithoutAllocation(em, fgiID, fgiQty);
        
        //Insert Loss
        entity = super.create(entity);
        return entity;
    }
    
    @POST
    @Path("doCellarLoss/{brewID}/{volume}")
    @Consumes({"application/xml", "application/json"})
    public LossLog createCellarLoss(@PathParam("brewID") int brewID,
            @PathParam("volume") float volume, @WebParam(name = "losslog") LossLog entity) {
        BrewsTools bt = new BrewsTools();
        bt.doVolumeRemoval(em, volume, brewID);
        
        //Insert Loss
        entity = super.create(entity);
        return entity;
    }
    
    @POST
    @Path("doBarrelLoss/{barrelID}")
    @Consumes({"application/xml", "application/json"})
    public LossLog createBarrelLoss(@PathParam("barrelID") int barrelID, @WebParam(name = "losslog") LossLog entity) {
        Barrels barrel = getBarrelFromID(barrelID);
        final Date nullDate = null;
        String history = barrel.getPastBatches();
        history += Integer.toString(barrel.getCurrentBatch().getBid()) + " on "
                + barrel.getDateFilled().toString() + ",";
        barrel.setCurrentVol(0);
        barrel.setCurrentBatch(null);
        barrel.setPastBatches(history);
        barrel.setDateFilled(nullDate);
        em.merge(barrel);
        
        //Insert Loss
        entity = super.create(entity);
        return entity;
    }
    
    @POST
    @Path("doFirkinLoss/{firkinID}")
    @Consumes({"application/xml", "application/json"})
    public LossLog createFirkinLoss(@PathParam("firkinID") int firkinID, @WebParam(name = "losslog") LossLog entity) {
        Firkins firkin = getFirkinFromID(firkinID);
        final java.util.Date nullDate = null;
        firkin.setFirkSold(true);
        firkin.setFirkResults("Firkin was destroyed because: " + entity.getLNotes());
        firkin.setFirkSalesDate(nullDate);
        em.merge(firkin);
        
        //Insert Loss
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(LossLog entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public LossLog find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<LossLog> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<LossLog> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    /***************************************************************************
     * Database access Functions.
     **************************************************************************/
    private Barrels getBarrelFromID(final int barrelID){
        TypedQuery<Barrels> query = em.createQuery(
                "SELECT b FROM Barrels b WHERE b.barID = ?1", Barrels.class);
        query.setParameter(1, barrelID);
        return query.getSingleResult();
    }
    
    private Firkins getFirkinFromID(final int firkinID){
        TypedQuery<Firkins> query = em.createQuery(
                "SELECT f FROM Firkins f WHERE f.firkID = ?1", Firkins.class);
        query.setParameter(1, firkinID);
        return query.getSingleResult();
    }
}
